import React, { useReducer } from 'react'

const Demo6 = () => {
    // useReducer 第一个参数的方法要放在 声明useReducer 之前
    const reducerCount = (state, action) => {
        switch (action) {
            case 'add':
                return state + 1
            case 'sub':
                return state - 1
            default:
                return state
        }
    }

    const [count, dispatch] = useReducer(reducerCount, 0)

    const handleSetCount = (type) => {
        dispatch(type)
    }

    return (
        <div>
            <p>当前数值: {count}</p>
            <button
                onClick={() => {
                    handleSetCount('add')
                }}
            >
                加
            </button>
            <button
                onClick={() => {
                    handleSetCount('sub')
                }}
            >
                减
            </button>
        </div>
    )
}

export default Demo6
